<?php

namespace My\Model;

use Utuo\UtuoModel;

class MallConfigModel extends UtuoModel {
	
	protected $_auto = array(
			array('reward_percent', 'getRewardPercent', self::MODEL_BOTH, 'callback'),
			array('reward_percent_dealer', 'getRewardPercentDealer', self::MODEL_BOTH, 'callback'),
	);
	
	// 自动验证
	protected $_validate = array(
			array('reward_percent', 'checkRewardPercent','基础佣金分成比例不正确', self::MUST_VALIDATE, 'callback'),
			array('reward_percent_dealer', 'checkRewardPercentDealer','金牌会员佣金分成比例不正确', self::MUST_VALIDATE, 'callback'),
	);

	public function checkRewardPercent() {
		$rewardPercent = I("reward_percent");
		$baseLevels = I("base_levels");
		if( empty($baseLevels) ) {
			$_POST['reward_percent'] = "";
		} else {
			if ( array_sum(explode(':', $rewardPercent)) != 100 ) {
				$this->error['reward_percent']  = '基础佣金分成比例之和必须等于100%';
				return false;
			}
		}
		return true;
	}
	
	public function checkRewardPercentDealer() {
		$rewardPercent = I("reward_percent_dealer");
		$dealerLevels = I("dealer_levels");
		if( empty($dealerLevels) ) {
			$_POST['reward_percent_dealer'] = "";
		} else {
			if ( array_sum(explode(':', $rewardPercent)) != 100 ) {
				$this->error['reward_percent_dealer'] = '金牌会员佣金分成比例之和必须等于100%';
				return false;
			}
		}
		return true;
	}
	
	/**
	 * 返拥金比例
	 */
	public function getRewardPercent() {
		$rewardPercent = I("reward_percent");
		$rewards = array();
		foreach ( explode(':', $rewardPercent) as $v ) {
			$rewards[] = $v*1;
		}
		return join(':', $rewards);
	}
	
	/**
	 * 返拥金比例
	 */
	public function getRewardPercentDealer() {
		$rewardPercent = I("reward_percent_dealer");
		$rewards = array();
		$tmp = explode(':', $rewardPercent);
		foreach ( $tmp as $v ) {
			$rewards[] = $v*1;
		}
		return join(':', $rewards);
	}
	
}